package platform.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 内容供应配置
 *
 * @author roamer
 * @version V1.0
 * @date 2019-02-24 20:27
 */
public class ProviderConfigurer {

    private List<Object> providers;

    private ObjectMapper objectMapper;

    public List<Object> getProviders() {
        return providers;
    }

    protected ObjectMapper getObjectMapper() {
        return objectMapper;
    }

    protected ProviderConfigurer(ObjectMapper objectMapper) {
        this.objectMapper = Optional.ofNullable(objectMapper).orElseGet(ProviderConfigurer::createObjectMapper);
        providers = new ArrayList<>();
        providers.add(new JacksonJsonProvider(this.objectMapper));
        providers.addAll(createParameterConversionProviders());
    }

    private static ObjectMapper createObjectMapper() {
        ObjectMapper objectMapper = null;
        try {
            objectMapper = new ObjectMapperConfigContext().objectMapper();
        } catch (Exception e) {
            throw new RuntimeException("Create ObjectMapper fail: ", e);
        }
        return objectMapper;
    }

    private static List<Object> createParameterConversionProviders() {
        return new ArrayList<>();
    }
}
